<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">企业名称：</div>
      <el-input v-model="queryParams.name" clearable placeholder="请输入内容" class="search-main" />
      <el-button type="primary" @click="searchCompany">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/park/enterprise/add')">添加企业</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="list" @expand-change="expandHandle">
        <el-table-column type="expand">
          <template #default="{ row }">
            <el-table :data="row.rentList">
              <el-table-column label="租赁楼宇" width="320" prop="name" />
              <el-table-column label="租赁起始时间" prop="startTime" />
              <el-table-column label="合同状态" prop="status">
                <template #default="scope">
                  <el-tag v-if="scope.row.status === 0" type="warning">待生效</el-tag>
                  <el-tag v-if="scope.row.status === 1" type="success">生效中</el-tag>
                  <el-tag v-if="scope.row.status === 2" type="info">已到期</el-tag>
                  <el-tag v-if="scope.row.status === 3" type="danger">已退租</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="180">
                <template #default="scope">
                  <el-button size="mini" type="text" :disabled="scope.row.status === 3" @click="goOutBtn(scope.row.id)">退租</el-button>
                  <el-button size="mini" type="text" :disabled="scope.row.status !== 3" @click="delEnterprise(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号" />
        <el-table-column label="企业名称" width="320" prop="name" />
        <el-table-column label="联系人" prop="contact" />
        <el-table-column
          label="联系电话"
          prop="contactNumber"
        />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini" type="text" @click="addBtn(scope.row.id)">添加合同</el-button>
            <el-button size="mini" type="text" @click="lookBtn(scope.row.id)">查看</el-button>
            <el-button size="mini" type="text" @click="editBtn(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delBtn(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :page-sizes="[5, 10, 20, 50, 100]"
        :page-size="queryParams.pageSize"
        @current-change="handleCurrentChange"
      />
    </div>
    <AddContract :id="curreentId" :dialog-visible.sync="dialogVisible" />
  </div>
</template>
<script>
import { enterpriseList, deleteEnterprise, getEnterpriseInfo } from '@/api/Enterprise'
import { OutBuilding } from '@/api/Building'
import AddContract from './components/add-contract'
export default {
  name: 'EnterprisePage',
  components: { AddContract },
  data() {
    return {
      list: [],
      buildingList: [],
      queryParams: {
        page: 1,
        pageSize: 5,
        name: null
      },
      total: 0,
      dialogVisible: false,
      curreentId: null,
      rentForm: {
        businessLicenseUrl: '',
        contractUrl: '',
        contractId: null,
        enterpriseId: null,
        type: 0,
        rentTime: [],
        buildingId: ''
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    async expandHandle(row, rows) {
      const isExpend = rows.find(item => item.id === row.id)
      if (isExpend) {
        const res = await getEnterpriseInfo(row.id)
        // eslint-disable-next-line require-atomic-updates
        row.rentList = res.data.rent
      }
    },
    async getList() {
      const { data: { rows, total }} = await enterpriseList(this.queryParams)
      this.list = rows.map(item => {
        return {
          ...item,
          rentList: []
        }
      })

      this.total = total
    },
    handleCurrentChange(val) {
      this.queryParams.page = val
      this.getList(this.queryParams)
    },
    searchCompany() {
      this.queryParams.page = 1
      this.getList(this.queryParams)
    },
    editBtn(id) {
      this.$router.push(`/park/enterprise/add/${id}`)
    },
    delBtn(id) {
      this.$confirm('是否确认删除当前企业?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteEnterprise(id)
        this.$message.success('删除成功')
        this.getList()
      })
    },
    addBtn(id) {
      this.dialogVisible = true
      this.curreentId = id
    },
    // 查看
    async lookBtn(id) {
      this.$router.push(`/park/enterprise/enterpriseInfo/${id}`)
    },
    delEnterprise(id) {
      console.log(id)
    },
    async goOutBtn(id) {
      this.$confirm('是否确认退租?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await OutBuilding(id)
        this.getList()
        this.$message.success('退租成功')
      }).catch(() => {
        this.$message.info('已取消')
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.department-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
